<template>
    <div class="levels-container" v-if="!showGameView">
        <!-- 页面标题 -->
        <div class="level-header">
            <h1 class="level-title">狂暴模式</h1>
            <button @click="goBack" class="back-button">
                <i class="fas fa-arrow-left"></i> 返回
            </button>
        </div>

        <!-- 关卡进度指示器 -->
        <div class="progress-tracker">
            <div class="progress-line"></div>
            <div v-for="(level, index) in levels" :key="index" 
                class="progress-node" 
                :class="{ 
                    active: currentLevel >= index + 1, 
                    unlocked: unlockedLevels >= index + 1,
                    current: currentLevel === index + 1
                }">
                {{ index + 1 }}
            </div>
        </div>

        <!-- 关卡展示区域 -->
        <div class="level-cards">
            <div v-for="(level, index) in levels" :key="index" 
                class="level-card"
                :class="{ 
                    locked: unlockedLevels < index + 1,
                    completed: completedLevels >= index + 1
                }">
                <div class="card-content">
                    <div class="level-number">关卡 {{ index + 1 }}</div>
                    <h3 class="level-name">{{ level.name }}</h3>
                    <p class="level-description">{{ level.description }}</p>
                    <div class="level-stats">
                        <div class="stat">
                            <i class="fas fa-star"></i>
                            <span>难度: {{ level.difficulty }}</span>
                        </div>
                        <div class="stat">
                            <i class="fas fa-trophy"></i>
                            <span>分值: {{ level.score }}</span>
                        </div>
                    </div>
                    <div class="level-status">
                        <span v-if="completedLevels >= index + 1" class="status-badge completed">
                            <i class="fas fa-check-circle"></i> 已完成
                        </span>
                        <span v-else-if="unlockedLevels >= index + 1" class="status-badge unlocked">
                            <i class="fas fa-unlock"></i> 已解锁
                        </span>
                        <span v-else class="status-badge locked">
                            <i class="fas fa-lock"></i> 未解锁
                        </span>
                    </div>
                    <button @click="enterLevel(index + 1)" 
                        class="enter-button"
                        :disabled="unlockedLevels < index + 1">
                        <span v-if="completedLevels >= index + 1">重新挑战</span>
                        <span v-else-if="unlockedLevels >= index + 1">开始挑战</span>
                        <span v-else>未解锁</span>
                    </button>
                </div>
                <div class="level-image">
                    <img :src="level.image" :alt="level.name" />
                    <div class="overlay" v-if="unlockedLevels < index + 1">
                        <i class="fas fa-lock fa-3x"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- 关卡详情弹窗 -->
        <div v-if="showLevelModal" class="level-modal-overlay" @click="closeLevelModal">
            <div class="level-modal" @click.stop>
                <div class="modal-header">
                    <h2>关卡 {{ selectedLevel }} - {{ levels[selectedLevel-1].name }}</h2>
                    <button @click="closeLevelModal" class="close-button">
                        <i class="fas fa-times"></i>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="level-image-large">
                        <img :src="levels[selectedLevel-1].image" :alt="levels[selectedLevel-1].name" />
                    </div>
                    <div class="level-details">
                        <p class="level-description-full">{{ levels[selectedLevel-1].fullDescription }}</p>
                        <div class="level-requirements">
                            <h4>挑战要求：</h4>
                            <ul>
                                <li v-for="(requirement, idx) in levels[selectedLevel-1].requirements" 
                                    :key="idx">
                                    {{ requirement }}
                                </li>
                            </ul>
                        </div>
                        <div class="level-rewards">
                            <h4>通关奖励：</h4>
                            <ul>
                                <li v-for="(reward, idx) in levels[selectedLevel-1].rewards" 
                                    :key="idx">
                                    {{ reward }}
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button @click="startGame" class="start-button">
                        开始游戏
                    </button>
                    <button @click="closeLevelModal" class="cancel-button">
                        取消
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 游戏视图区域 -->
    <div v-else class="game-view-container">
        <div class="game-header">
            <div class="level-info">
                <h2>{{ levels[currentLevel-1].name }}</h2>
                <div class="level-stats-small">
                    <div class="stat">
                        <i class="fas fa-star"></i>
                        <span>难度: {{ levels[currentLevel-1].difficulty }}</span>
                    </div>
                    <div class="stat">
                        <i class="fas fa-trophy"></i>
                        <span>分值: {{ levels[currentLevel-1].score }}</span>
                    </div>
                </div>
            </div>
            <button @click="exitGame" class="exit-button">
                <i class="fas fa-times"></i> 退出游戏
            </button>
        </div>
        
        <!-- 根据当前关卡动态加载对应的组件 -->
        <div class="game-content">
            <SingleGround v-if="currentLevel === 1 || currentLevel === 2 || currentLevel === 3" :level="currentLevelName" @complete="completeLevel(currentLevel)" />
            <div v-else class="placeholder-game">
                <p>未知关卡内容</p>
            </div>
        </div>
    </div>
</template>

<script>
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue'
import { useRouter } from 'vue-router'

// 异步加载关卡组件
const SingleGround = defineAsyncComponent(() => 
    import('@/views/game/pk/single/SingleGround.vue')
)

export default {
    name: 'SinglePkView',
    components: {
        SingleGround
    },
    setup() {
        const router = useRouter()
        const levels = reactive([
            {
                name: '被球球围攻啦',
                description: '锻炼你对技能的熟练度',
                fullDescription: '欢迎来到BallBall的世界！在这个关卡中，你将学习熟练基本的操作和规则。击败对手获得胜利，解锁下一关！',
                difficulty: '★☆☆',
                score: 30,
                image: 'https://placehold.co/600x400/2a2a2a/00ff88?text=Level+1',
                requirements: [
                    '使用技能击败对手',
                    '不死亡',
                    
                ],
                rewards: [
                    '解锁下一关',
                    '获得30分'

                ]
            },
            {
                name: '击败所有球球',
                description: '挑战更多类型的对手，掌握进阶技巧',
                fullDescription: '恭喜你通过了第一关！现在面临更强的对手，你需要掌握更多高级技巧和策略。发挥你的实力，继续前进！',
                difficulty: '★★☆',
                score: 40,
                image: 'https://placehold.co/600x400/2a2a2a/00ff88?text=Level+2',
                requirements: [
                    '击败所有球球',
                    '在一分钟内击败所有球球'
                ],
                rewards: [
                   '解锁下一关',
                   '获得40分'
                ]
            },
            {
                name: '终极之站',
                description: '与终极球球过招，检验你的真实实力',
                fullDescription: '最终的挑战！这里集结了最强大的对手，考验你的反应、策略和技巧。胜利将证明你是真正的BallBall大师！',
                difficulty: '★★★',
                score: 50,
                image: 'https://placehold.co/600x400/2a2a2a/00ff88?text=Level+3',
                requirements: [
                   '击败终极球球',
                   '在一分钟内击败终极球球'
                ],
                rewards: [
                    '解锁下一关',
                    '获得50分'
                ]
            }
        ])

        // 状态变量
        const currentLevel = ref(1)
        const unlockedLevels = ref(1)  // 默认只解锁第一关
        const completedLevels = ref(0) // 初始没有完成的关卡
        const selectedLevel = ref(null)
        const showLevelModal = ref(false)
        const showGameView = ref(false) // 控制是否显示游戏视图
        const currentLevelName = ref('first')

        // 返回上一页
        const goBack = () => {
            router.go(-1)
        }

        // 进入关卡
        const enterLevel = (levelNumber) => {
            if (unlockedLevels.value >= levelNumber) {
                selectedLevel.value = levelNumber
                showLevelModal.value = true
            }
        }

        // 关闭关卡弹窗
        const closeLevelModal = () => {
            showLevelModal.value = false
        }

        // 开始游戏
        const startGame = () => {
            currentLevel.value = selectedLevel.value
            
            // 根据关卡设置难度
            switch(currentLevel.value) {
                case 1:
                    currentLevelName.value = 'first'
                    break
                case 2:
                    currentLevelName.value = 'second'
                    break
                case 3:
                    currentLevelName.value = 'third'
                    break
                default:
                    currentLevelName.value = 'first'
            }
            
            console.log('开始游戏，当前关卡:', currentLevel.value, '难度级别:', currentLevelName.value)
            
            // 关闭弹窗
            closeLevelModal()
            
            // 显示游戏界面
            showGameView.value = true
        }

        // 退出游戏
        const exitGame = () => {
            // 通过动态加载的方式使用GameObject
            try {
                const GameObject = require('@/assets/scripts/GameObject').GameObject;
                if (GameObject && typeof GameObject.clearAll === 'function') {
                    GameObject.clearAll();
                }
            } catch (error) {
                console.warn('退出游戏时清理游戏对象出错:', error);
            }
            
            showGameView.value = false;
        }

        // 完成关卡
        const completeLevel = (levelNumber) => {
            if (completedLevels.value < levelNumber) {
                completedLevels.value = levelNumber
                
                // 如果是新完成的关卡并且不是最后一关，则解锁下一关
                if (levelNumber < levels.length) {
                    unlockedLevels.value = Math.max(unlockedLevels.value, levelNumber + 1)
                    setTimeout(() => {
                        alert(`恭喜通关！已解锁关卡 ${levelNumber + 1}`)
                        // 可以选择自动退回到选关界面
                        exitGame();
                    }, 500)
                } else {
                    setTimeout(() => {
                        alert('恭喜你完成了所有关卡！你是真正的大师！')
                        exitGame();
                    }, 500)
                }
            } else {
                // 关卡已完成过，仍需调用exitGame确保清理
                exitGame();
            }
        }

        // 生命周期钩子
        onMounted(() => {
            // 可以从本地存储或后端加载已解锁和已完成的关卡
            // 这里仅作演示
        })

        return {
            levels,
            currentLevel,
            unlockedLevels,
            completedLevels,
            selectedLevel,
            showLevelModal,
            showGameView,
            currentLevelName,
            goBack,
            enterLevel,
            closeLevelModal,
            startGame,
            exitGame,
            completeLevel
        }
    }
}
</script>

<style scoped>
.levels-container {
    min-height: 100vh;
    background: #0a0a0a;
    color: #e0e0e0;
    padding: 2rem;
    position: relative;
}

.level-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.level-title {
    color: #00ff88;
    font-size: 2.5rem;
    text-shadow: 0 0 10px rgba(0, 255, 136, 0.3);
    margin: 0;
}

.back-button {
    background: rgba(255, 255, 255, 0.05);
    color: #00ff88;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.back-button:hover {
    background: rgba(0, 255, 136, 0.1);
    transform: scale(1.05);
}

/* 进度指示器样式 */
.progress-tracker {
    display: flex;
    justify-content: space-between;
    position: relative;
    margin: 3rem auto;
    max-width: 800px;
    padding: 0 4rem;
}

.progress-line {
    position: absolute;
    top: 50%;
    left: 6rem;
    right: 6rem;
    height: 3px;
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-50%);
    z-index: 1;
}

.progress-node {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #2a2a2a;
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: #aaa;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    position: relative;
    z-index: 2;
    transition: all 0.3s ease;
}

.progress-node.unlocked {
    background: #444;
    border-color: #00ff88;
    color: #00ff88;
}

.progress-node.active {
    background: #00ff88;
    color: #000;
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}

.progress-node.current {
    animation: pulse 2s infinite;
}

/* 关卡卡片样式 */
.level-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 2rem;
    margin-top: 3rem;
    margin-left: 10rem;
    margin-right: 10rem;
}

.level-card {
    background: #1a1a1a;
    border-radius: 12px;
    overflow: hidden;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    height: 450px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}

.level-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.level-card.locked {
    filter: grayscale(0.8);
    opacity: 0.7;
}

.level-card.completed {
    border: 2px solid #00ff88;
}

.card-content {
    padding: 1.5rem;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.level-number {
    font-size: 0.9rem;
    color: #aaa;
    margin-bottom: 0.5rem;
}

.level-name {
    font-size: 1.5rem;
    color: #00ff88;
    margin: 0 0 0.8rem 0;
}

.level-description {
    font-size: 1rem;
    color: #ddd;
    margin-bottom: 1rem;
    flex: 1;
}

.level-stats {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
}

.stat {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #aaa;
}

.stat i {
    color: #00ff88;
}

.level-status {
    margin-bottom: 1rem;
}

.status-badge {
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    font-size: 0.9rem;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.status-badge.completed {
    background: rgba(0, 255, 136, 0.1);
    color: #00ff88;
}

.status-badge.unlocked {
    background: rgba(255, 174, 0, 0.1);
    color: #ffae00;
}

.status-badge.locked {
    background: rgba(255, 255, 255, 0.05);
    color: #aaa;
}

.enter-button {
    margin-top: auto;
    background: linear-gradient(90deg, #00ff88, #00ccff);
    color: #000;
    border: none;
    padding: 0.8rem;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
}

.enter-button:hover:not(:disabled) {
    transform: scale(1.03);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}

.enter-button:disabled {
    background: #444;
    color: #888;
    cursor: not-allowed;
}

.level-image {
    height: 180px;
    position: relative;
    overflow: hidden;
}

.level-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease;
}

.level-card:hover .level-image img {
    transform: scale(1.1);
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
}

/* 关卡弹窗样式 */
.level-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    backdrop-filter: blur(5px);
}

.level-modal {
    background: #1a1a1a;
    border-radius: 12px;
    width: 90%;
    max-width: 800px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
}

.modal-header {
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header h2 {
    color: #00ff88;
    margin: 0;
}

.close-button {
    background: none;
    border: none;
    color: #ccc;
    font-size: 1.5rem;
    cursor: pointer;
    transition: color 0.3s ease;
}

.close-button:hover {
    color: #00ff88;
}

.modal-body {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.level-image-large {
    height: 250px;
    border-radius: 8px;
    overflow: hidden;
}

.level-image-large img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.level-details {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.level-description-full {
    line-height: 1.6;
    color: #ddd;
}

.level-requirements, .level-rewards {
    background: rgba(255, 255, 255, 0.05);
    padding: A1.5rem;
    border-radius: 8px;
}

.level-requirements h4, .level-rewards h4 {
    color: #00ff88;
    margin-top: 0;
}

.level-requirements ul, .level-rewards ul {
    padding-left: 1.5rem;
    color: #ddd;
}

.level-requirements li, .level-rewards li {
    margin-bottom: 0.5rem;
}

.modal-footer {
    padding: 1.5rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
}

.start-button {
    background: linear-gradient(90deg, #00ff88, #00ccff);
    color: #000;
    border: none;
    padding: 0.8rem 2rem;
    border-radius: 6px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
}

.start-button:hover {
    transform: scale(1.05);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.3);
}

.cancel-button {
    background: rgba(255, 255, 255, 0.1);
    color: #ddd;
    border: none;
    padding: 0.8rem 2rem;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.cancel-button:hover {
    background: rgba(255, 255, 255, 0.15);
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(0, 255, 136, 0.7);
    }
    70% {
        box-shadow: 0 0 0 15px rgba(0, 255, 136, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(0, 255, 136, 0);
    }
}

@media (min-width: 768px) {
    .modal-body {
        flex-direction: row;
    }
    
    .level-image-large {
        flex: 1;
        height: auto;
    }
    
    .level-details {
        flex: 1;
    }
}

/* 游戏视图样式 */
.game-view-container {
    min-height: 100vh;
    height: 100vh;
    background: #0a0a0a;
    color: #e0e0e0;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background: rgba(0, 0, 0, 0.5);
    border-bottom: 1px solid rgba(0, 255, 136, 0.2);
    z-index: 10;
}

.level-info {
    display: flex;
    flex-direction: column;
}

.level-info h2 {
    color: #00ff88;
    margin: 0;
}

.level-stats-small {
    display: flex;
    gap: 1rem;
    margin-top: 0.5rem;
}

.exit-button {
    background: rgba(255, 255, 255, 0.05);
    color: #ff4d4d;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.exit-button:hover {
    background: rgba(255, 77, 77, 0.1);
    transform: scale(1.05);
}

.game-content {
    flex: 1;
    position: absolute;
    top: 60px; /* 根据header高度调整 */
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    overflow: hidden;
}

.placeholder-game {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    color: #aaa;
    background: #121212;
}
</style>
